<template>
    <div>
        <h3 class="bolder red center" >
            <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
            <router-link to="/business/course" class="pink"> {{course.name}} </router-link>
        </h3>
        <p>
            <router-link class="btn btn-white btn-default btn-round" to="/business/course">
                <i class="ace-icon fa fa-arrow-left grey"></i>
                返回
            </router-link>
            &nbsp;
            <button class="btn btn-white btn-default btn-round" @click="refresh()">
                <i class="ace-icon fa fa-refresh red"></i>
                刷新
            </button>
            &nbsp;
            <button class="btn btn-white btn-success btn-round" @click="openAddModal()">
                <i class="ace-icon fa fa-edit blue"></i>
                新增
            </button>
        </p>

        <table id="simple-table" class="table  table-bordered table-hover table-responsive " >
            <thead>
            <tr>
                <th class="center" style="width: 150px">ID</th>
                <th class="center" style="width: 300px">名称</th>
<!--                <th class="center">课程</th>-->
                <th class="center" style="width: 150px">排序</th>
                <th class="center">操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="chapter in chapters">
                <td class="center">{{chapter.id}}</td>
                <td class="center">{{chapter.name}}</td>
<!--                <td class="center">{{course.name}}</td>-->
                <td class="center">{{chapter.sort}}</td>
                <td class="center" style="width:200px;">
                    <div class=" btn-group">
                        <button v-on:click="toSection(chapter)" class="btn btn-white btn-xs btn-info btn-round">
                            小节
                        </button>&nbsp;
                        <button v-on:click="openEditModal(chapter.id)" class="btn btn-white btn-xs btn-info btn-round">
                            <i class="ace-icon fa fa-edit bigger-120"></i>
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <button v-on:click="deleteById(chapter.id)" class="btn btn-white btn-xs btn-danger btn-round">
                            <i class="ace-icon fa fa-remove bigger-120"></i>
                        </button>
                    </div>

                </td>
            </tr>
            <tr v-if="chapters.length === 0">
                <td colspan="4" class="center" style="background-color: lightgrey;font-family: 'Courier New',serif">
                    暂无数据
                </td>
            </tr>
            </tbody>
        </table>
        <!-- 分页组件 -->
        <pagination ref="pagination" v-bind:list="list"></pagination>

        <!-- 模态框 -->
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content" style="border-color: #aa160e;background-color: lightblue">
                    <div class="modal-header">
                        <button type="button" class="close blue" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title grey bolder"
                            id="myModalLabel">{{modalTitle}}</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       style="font-family: 'Courier New', Courier, monospace"> 名称 </label>

                                <div class="col-sm-9">
                                    <input v-model="chapter.name" type="text" placeholder="大章名称"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       > 课程</label>
                                <div class="col-sm-9">
                                    <input disabled v-model="course.name" type="text" placeholder="课程"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       > 排序 </label>

                                <div class="col-sm-9">
                                    <input oninput = "value=value.replace(/[^\d]/g,'')" v-model="chapter.sort" type="text" placeholder="排序"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-round"
                                 data-dismiss="modal">
                            关闭
                        </button>
                        <button @click="save()" type="button" class="btn btn-primary btn-round"
                                >
                            保存
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
    import Pagination from "../../components/pagination.vue"

    export default {
        name: "chapter",
        components: {
            Pagination
        },
        data: function () {
            return {
                chapters: [],
                chapter: {
                    name: "",
                    courseId: ''
                },
                modalTitleArr: {
                    insert: "新增大章",
                    update: "修改大章"
                },
                modalTitle: "",
                course: {}
            }
        },
        mounted: function () {
            // this.$parent.activeSidebar("business-chapter-sidebar")
            let _this = this;
            let course = SessionStorage.get(SESSION_KEY_COURSE) || {};
            if (Tool.isEmpty(course)) {
                _this.$router.push("/welcome");
            }
            _this.course = course;
            _this.list();
            //激活课程的样式
            _this.$parent.activeSidebar("business-course-sidebar");
        },
        methods: {
            /* 打开新增模态框 */
            openAddModal() {
                let _this = this;
                _this.clearChapter();
                _this.modalTitle = _this.modalTitleArr.insert;
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");

            },
            /*打开编辑模态框*/
            openEditModal(id) {
                let _this = this;
                _this.clearChapter();
                _this.modalTitle = _this.modalTitleArr.update;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/business/admin/chapter/findById/' + id).then((response) => {
                    _this.chapter = response.data.data;
                });
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");
            },
            refresh() {
                let _this = this;
                Loading.show();
                _this.list(_this.$refs.pagination.page);
                Loading.hide();
            },
            /* 查找大章 */
            list(page = 1) {
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/chapter/list/'+_this.course.id, {
                    page: page,
                    size: _this.$refs.pagination.size
                }).then((response) => {
                    _this.chapters = response.data.data.list;
                    _this.$refs.pagination.render(page, response.data.data.total);
                })
            },
            /*保存大章信息到后台*/
            save() {
                let _this = this;
                if (!_this.chapter.id) {
                    /*不存在id为新增*/
                    _this.insert();
                } else {
                    /*存在id为修改*/
                    _this.update();
                }
            },
            /*新增大章*/
            insert() {
                let _this = this;
                /*新增校验*/
                if (!Validator.require(_this.chapter.name, "名称")
                    || !Validator.length(_this.chapter.courseId, "课程", 1, 19)) {
                    return;
                }
                _this.chapter.courseId = _this.course.id;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/chapter/save', _this.chapter).then((response) => {
                    console.log(response);
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                });
            },
            /*修改大章*/
            update() {
                let _this = this;
                /*更新校验*/
                if (!Validator.require(_this.chapter.name, "名称")
                    && !Validator.require(_this.chapter.courseId, "课程")
                    || !Validator.length(_this.chapter.courseId, "课程ID", 1, 19)) {
                    return;
                }
                _this.$ajax.put(process.env.VUE_APP_SERVER + '/business/admin/chapter/update', _this.chapter).then((response) => {
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                })
            },
            /*删除大章*/
            deleteById(id) {
                let _this = this;
                /*删除提示框*/
                Confirm.show("", () => {
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/chapter/deleteById/' + id).then((response) => {
                        if (response.data.success) {
                            _this.list(_this.$refs.pagination.page);
                            Toast.success(response.data.message);
                        } else {
                            Toast.error(response.data.message);
                        }
                        $("#myModal1").modal("hide");
                    })
                });
            },
            /*清空模态框的数据*/
            clearChapter() {
                let _this = this;
                _this.chapter = {};
                _this.modalTitle = "";
            },
            /*跳转到小节*/
            toSection(chapter) {
                let _this = this;
                SessionStorage.set(SESSION_KEY_CHAPTER, chapter);
                _this.$router.push("/business/section");
            }
        }
    }
</script>
